// pages/storeShop/storeShop.js
var app = getApp();
var util = require('../../utils/util');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        array: ['受益人', '申请人', '经办人'],
        index: 0,
        getCodeName: '获取验证码',
        countdown: 60,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
		
        if (options.q) {
            let q = util.getJson(decodeURIComponent(options.q));
			
            if (q.letter_id != undefined) {
                //爱心分享邀请用户
                that.setData({
                    letter_id: q.letter_id,
					type:q.type
                });
            }else{
				util.message('请扫描正确的二维码');
			}
        } else {
            that.setData({
                letter_id: options.letter_id,
				type: options.type
            });
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },
    bindChangeType: function(e) {
        var that = this;
        that.setData({
            index: e.detail.value,
        });
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var that = this;
    },
    // 输入手机号
    changePhone: function(e) {
        // console.log(e);
        var that = this;
        that.setData({
            mobile: e.detail.value,
        });
    },
    // 输入验证码
    changeCode: function(e) {
        var that = this;
        that.setData({
            code: e.detail.value,
        });
    },

    MakeCode: function() {
        var that = this;
        var phoneValue = that.data.mobile;
        if (!(/^1[3456789]\d{9}$/.test(phoneValue))) {
            util.showSuccess("手机号码有误，请重填", 'none', false);
            return false;
        } else {
            util.showLoading('发送中...');
            var apiUrl = 'api/v1/common/identifying_code';
            var apiData = {
                phone: phoneValue,
            }
            util.http(apiUrl, apiData,function(res){
                if (res.state != 1) {
                    util.hideLoading();
                    util.message(res.datas);
                    return false;
                } else {
                    util.hideLoading();
                    that.settime();
                }
            });
        }
    },

    settime: function() {
        var that = this;
        var countdown = that.data.countdown;
        if (countdown === 1) {
            that.setData({
                disabled: false,
                getCodeName: '验证码',
                countdown: 60,
            });
            return;
        } else {
            var lasttime = parseInt(countdown) - parseInt(1);
            that.setData({
                disabled: true,
                getCodeName: '验证码(' + lasttime + ')',
                countdown: lasttime,
            });
        }
        setTimeout(function() {
            that.settime();
        }, 1000);
    },
    makeLogin: function() {
        var that = this;
        var phoneValue = that.data.mobile;
        if (!(/^1[3456789]\d{9}$/.test(phoneValue))) {
            util.message("手机号码有误，请重填");
            return false;
        }
       // console.log(that.data.code);
		if (that.data.code == undefined) {
            util.message("验证码格式有误，请重填");
            return false;
        }
        util.showLoading('验证中...');
        var apiUrl = 'api/v1/common/check_identifying_code';
        var apiData = {
            phone: phoneValue,
            code: that.data.code,
        }
        util.http(apiUrl, apiData,function(res) {
            if (res.state == 1) {
                util.hideLoading();
				var parm="";
				if (that.data.type == "undefined"){
					parm = {
						mobile: phoneValue,
						shenfen: that.data.array[that.data.index],
						letter_id: that.data.letter_id,
					}
				}else{
					parm = {
						mobile: phoneValue,
						shenfen: that.data.array[that.data.index],
						letter_id: that.data.letter_id,
						type: that.data.type,
					}
				}
			//	console.log(parm,"参数")
				util.http("/api/v1/common/add_see_log",parm,function(ress){
					if(ress.state==1){
						wx.navigateTo({
							url: '/pages/letter/index?letter_id=' + that.data.letter_id + "&type=" + that.data.type,
						});
					}
					
				})
               
                return false;
            } else {
                util.hideLoading();

                util.message(res.datas);
            }
        });

    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})